package com.nwpu.coursesupport.view.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.controller.AbstractController;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.StatusType;
import com.nwpu.coursesupport.service.imple.StudentGradeViewService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.ExcelBuilderUtil;
import com.nwpu.coursesupport.tool.WebUtils;
import com.nwpu.coursesupport.view.StudentGradeView;

@Controller
@RequestMapping("/studentGradeView")
public class StudentGradeViewController extends AbstractController{
	
	@Autowired
	private StudentGradeViewService studentGradeViewService;
	
	@RequestMapping("/ajaxListData")
	@ResponseBody
	public JSONResult ajaxListData(@RequestParam("data")String data){
		JSONResult rst=invokeService("studentGradeView", "listData", data);
		return rst;
	}
	
	@RequestMapping("/listData")
	public String listData(@RequestParam("courseId")int courseId,HttpServletRequest request){
		JSONResult rst=invokeService("studentGradeView", "list", "{\"courseId\":\"" + courseId + "\"}");
		logger.error("courseId"+courseId);
		request.setAttribute("data", this.parseData(rst));
		return "/teapage/courseScoreIndex";
	}
	
	@RequestMapping("/gradeView")
	public String gradeView(@RequestParam("classId")int classId,HttpServletRequest request){
		JSONResult rst=invokeService("studentGradeView", "stuGradeDetail", "{\"id\":\"" + classId + "\"}");
		request.setAttribute("data", this.parseData(rst));
		return "/stupage/courseScore";

	}

//	@RequestMapping("/targetView")
//	public String targetView(@RequestParam("classId")int classId,HttpServletRequest request){
//		JSONResult rst=invokeService("studentGradeView", "stuGradeDetail", "{\"id\":\"" + classId + "\"}");
//		request.setAttribute("data", this.parseData(rst));
//		return "/stupage/courseScore";
//	}
	
	@RequestMapping("/genGradeExcel")
	public void genGradeExcel(@RequestParam("classId")int classId,HttpServletResponse response){
		JSONResult rst=new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(classId);
		List<StudentGradeView> list= studentGradeViewService.gradeListData(p, rst);
		if(list==null){
			WebUtils.sendJson(response, StatusType.WRONG,rst.getMessage());
		}else{
			String [] title={"班级","学号","姓名","任务成绩","平时成绩","理论成绩","实验成绩","总成绩"};
			String [][] content=new String[list.size()][title.length];
			for(int i=0;i<list.size();i++){
				int index=0;
				StudentGradeView item=list.get(i);
				content[i][index++]=item.getClassNumber();
				content[i][index++]=item.getStudentCode();
				content[i][index++]=item.getName();
				content[i][index++]=String.valueOf(item.getTaskTotalGrade());
				content[i][index++]=String.valueOf(item.getDailyGrade());
				content[i][index++]=String.valueOf(item.getTheoryTestGrade());
				content[i][index++]=String.valueOf(item.getLabTestGrade());
				content[i][index++]=String.valueOf(item.getFinalTotalGrade());
			}
			ExcelBuilderUtil.createGradeExcel(list.get(0).getClassName()+"-成绩单", content,title, response);
		}
	}
	
}
